package banking;

/**
 * @author xbd
 * @create 2021-06-03 9:22
 */
public class CheckingAccount extends Account{
    private double overdraftProtection;
    private SavingAccount protectedBy;

    public CheckingAccount(double init_balance) {
        super(init_balance);
    }

    public CheckingAccount(double init_balance, double overdraftProtection) {
        super(init_balance);
        this.overdraftProtection = overdraftProtection;
    }

    public CheckingAccount(double init_balance,SavingAccount protectedBy){
        super(init_balance);
        this.protectedBy = protectedBy;
    }

    public SavingAccount getProtectedBy() {
        return protectedBy;
    }

    public void setProtectedBy(SavingAccount protectedBy) {
        this.protectedBy = protectedBy;
    }

    public double getOverdraftProtection() {
        return overdraftProtection;
    }

    public void setOverdraftProtection(double overdraftProtection) {
        this.overdraftProtection = overdraftProtection;
    }

    @Override
    public boolean withdraw(double amt) {
        if(balance > amt){
            balance -= amt;
           // return true;
        }else if(protectedBy != null && protectedBy.getBalance() >= (amt - balance)){
            protectedBy.withdraw(amt - balance);
            balance = 0;
           // return true;
        }else{
            return false;
        }
        return true;
    }
}
